package com.opmsys.controller;

import com.opmsys.pojo.dto.PagingSearchAboutMaintain;
import com.opmsys.pojo.entity.Maintain;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.MaintainService;
import com.opmsys.utils.jsonWebToken.JsonWebTokenUtil;
import com.opmsys.utils.redis.RedisUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/maintain")
public class MaintainController {
    private final MaintainService maintainService;
    private final JsonWebTokenUtil jsonWebTokenUtil;
    private final RedisUtil redisUtil;

    public MaintainController(MaintainService maintainService, JsonWebTokenUtil jsonWebTokenUtil, RedisUtil redisUtil) {
        this.maintainService = maintainService;
        this.jsonWebTokenUtil = jsonWebTokenUtil;
        this.redisUtil = redisUtil;
    }


    /**
     * 用于管理员录入新的保养
     * @param maintain
     * @return
     */
    @PostMapping()
    public Result add(Maintain maintain){
        boolean isAddSuccess  = maintainService.addMaintain(maintain);
        if (isAddSuccess){
            return Result.successWithMsg("添加保养记录成功！");
        }else {
            return Result.error("添加保养记录失败！");
        }
    }



    /**
     * 用于管理员更新人员信息
     * @param maintain
     * @return
     */
    @PutMapping()
    public Result update(Maintain maintain){
        boolean isUpdateSuccess  = maintainService.upateMaintain(maintain);
        if (isUpdateSuccess){
            return Result.successWithMsg("更新保养记录成功！");
        }else {
            return Result.error("更新保养记录失败！");
        }
    }

    /**
     * 保养记录分页查询
     * @param pagingSearchAboutMaintain
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> search(PagingSearchAboutMaintain pagingSearchAboutMaintain){
        List<Maintain> list = maintainService.selectMaintain(pagingSearchAboutMaintain);
        long total = maintainService.pageTotal(pagingSearchAboutMaintain);
        PageResult pageResult = new PageResult(total,list);
        return Result.successWithMsgAndData("查询成功",pageResult);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping()
    public Result delete(@RequestParam(required = false) List<Integer> ids){
        int deleteNum  = maintainService.deleteMaintain(ids);
        return Result.successWithMsg("删除保养"+deleteNum+"条记录！");

    }

}
